package com.qianchang.microfinance.view.mine.bill;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.RepayPlanAfterParams;
import com.qianchang.microfinance.bean.RepaymentPlanAfterBean;
import com.qianchang.microfinance.bean.RepaymentPlanBean;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.base.EmptyLayout;
import com.qianchang.microfinance.view.base.RecyclerRefreshLayout;
import com.qianchang.microfinance.view.mine.loan.RepayPlanActivity;
import com.qianchang.microfinance.view.mine.loan.RepaymentPlanAdapter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoanPlanActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.error_layout)
    EmptyLayout errorLayout;
    @BindView(R.id.refreshLayout)
    RecyclerRefreshLayout refreshLayout;
    @BindView(R.id.tv_total)
    DTextView tvTotal;
    @BindView(R.id.tv_total_interest_id)
    DTextView tv_total_interest_id;
    @BindView(R.id.tv_audit_fee_id)
    DTextView tv_audit_fee_id;
    @BindView(R.id.tv_actual_arrival_id)
    DTextView tv_actual_arrival_id;
    private String deadline;
    private String repaymentType;
    private double rate;
    private String serviceFeeSetting;

    private boolean isLoanBefore;// true 是贷前
    private String loanAmount;
    private String projectId;
    private RepaymentPlanAdapter adapter;
    private List mList = new ArrayList();
    private String strRepayMode;//贷后传来的 还款方式
    private String strInterestRate;
    private String repayModeId;
    private RepayPlanAfterParams planAfterParams;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_renew_loan_plan);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(("续贷还款计划"));
        //生成
        Bundle bundle = getBundle();
        repaymentType = bundle.getString("repaymentType");
        deadline = bundle.getString("deadline");//借款期限
        rate = bundle.getDouble("reta");//利率（月）
        loanAmount = bundle.getString("applyAmount");//借款金额
        initList();
        tvTotal.setTextContent(FormatUtil.numKbPointFormat(loanAmount));
    }

    @Override
    protected void initData() {
        super.initData();

        getPlanBefore();
    }
    private void initList() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RepaymentPlanAdapter(LoanPlanActivity.this, mList, R.layout.adapter_repay_plan);
        recyclerView.setAdapter(adapter);
    }
    private void getPlanBefore() {
        HttpParams params = new HttpParams();
        params.put("applyAmount", loanAmount);
        params.put("repaymentType",repaymentType);
        params.put("deadline", deadline);
        params.put("reta", rate);
        params.put("serviceFeeSetting", "1");//
        show();
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.REPAYMENT_PLAN_BEFORE, params, new RequestCallBack<RepaymentPlanBean>() {
            @Override
            public void onSuccess(JsonResult<RepaymentPlanBean> response) {
                dismiss();
                refreshLayout.setRefreshing(false);
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    RepaymentPlanBean bean = response.getObject();
                    if (bean.getPlanList() != null) {
//                        tv_total_interest_id.setTextContent(FormatUtil.numKbPointFormat(bean.getLixiTotal()));
//                        tv_audit_fee_id.setTextContent(FormatUtil.numKbPointFormat(bean.getGqf()));
//                        tv_actual_arrival_id.setTextContent(FormatUtil.numKbPointFormat(bean.getServiceFee()));
                        tv_total_interest_id.setTextContent(bean.getLixiTotal());
                        tv_audit_fee_id.setTextContent(bean.getGqf());
                        tv_actual_arrival_id.setTextContent(bean.getServiceFee());
                        refresh(bean.getPlanList());
                    }
                }
                else {
                    ToastUtil.getInstant().show(LoanPlanActivity.this, response.getDescription());
                }

            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                refreshLayout.setRefreshing(false);
                ToastUtil.getInstant().show(LoanPlanActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

    private void refresh(List planList) {
        if (planList == null) return;
        mList.clear();
        mList.addAll(planList);
        adapter.notifyDataSetChanged();






    }
}
